package com.hangkong.controller;

import com.hangkong.model.BackResult;
import com.hangkong.model.tuser;
import com.hangkong.service.showUserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2017/9/5.
 */
@Controller
@RequestMapping("/user")
public class showUserController {
    private Logger log = Logger.getLogger(showUserController.class);
    @Resource
    private showUserService showUserService;

    @RequestMapping("/showUser")
    @ResponseBody
    public List<tuser> showUser() {
        log.info("查询所有订单信息");
        List<tuser> userList = showUserService.getallUser();
        return userList;
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public int deleteUser(@RequestParam(value = "idss[]") String[] idss) {
        log.info("删除用户");
        System.out.print(idss);
        for (String id : idss) {
            showUserService.deleteUser(id);
        }
        return 1;
    }


    @RequestMapping("/user_query")
    @ResponseBody
    public List<tuser> user_query(HttpServletRequest request) {
        log.info("条件查询用户");
        String select_name = request.getParameter("select_name");

        List<tuser> userList = showUserService.user_query(select_name);
        System.out.printf(userList.toString());

        return userList;

    }

    @RequestMapping("/save")
    @ResponseBody
    public String update_user(HttpServletRequest request) {
        log.info("修改用户信息");
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.print(username);
        showUserService.update_user(id, username, password);
        return "s";
    }


    @RequestMapping("/old")
    @ResponseBody
    public tuser showItem(String id) {
        tuser user = showUserService.showItem(id);
        return user;
    }


    @RequestMapping("/checkphone")
    @ResponseBody
    public BackResult checkphone(String phone) {
        log.info("核对手机号");
        System.out.print(phone);
        BackResult result =showUserService.checkphone(phone);

        return result;

    }


    @RequestMapping("/register")
    @ResponseBody
    public BackResult saveuser(HttpServletRequest request){
        log.info("注册用户");
        String name=request.getParameter("name");
        String email=request.getParameter("email");
        String phone=request.getParameter("phone");
        BackResult result=showUserService.saveuser(name,email,phone);
        return result;

    }
   @RequestMapping("/login1")
    @ResponseBody
    public BackResult loginuser(String name,String pwd,HttpServletRequest request, HttpServletResponse response){

       log.info("用户登录");
       BackResult result=showUserService.loginuser(name,pwd,request,response);

       return  result;
   }


}